﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class UserInsertViewModel : NotificationObject
    {
        UserInsert gWindow;
        UserViewModel gParentViewModel;

        public UserInsertViewModel(UserInsert pWindow, UserViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            Sexs.Add("男");
            Sexs.Add("女");
            Sexs.Add("其他");
        }

        private String openID;
        public String OpenID
        {
            get
            {
                return openID;
            }
            set
            {
                openID = value;
                RaisePropertyChanged("OpenID");
            }
        }

        private String userName;
        public String UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                RaisePropertyChanged("UserName");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private String nickname;
        public String Nickname
        {
            get
            {
                return nickname;
            }
            set
            {
                nickname = value;
                RaisePropertyChanged("Nickname");
            }
        }

        private List<String> sexs = new List<String>();
        public List<String> Sexs
        {
            get
            {
                return sexs;
            }

            set
            {
                sexs = value;
                RaisePropertyChanged("Sexs");
            }
        }

        private String sexsSelected;
        public String SexsSelected
        {
            get
            {
                return sexsSelected;
            }

            set
            {
                sexsSelected = value;
                RaisePropertyChanged("SexsSelected");
            }
        }

        private String province;
        public String Province
        {
            get
            {
                return province;
            }
            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private String city;
        public String City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        private String country;
        public String Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
                RaisePropertyChanged("Country");
            }
        }

        private String headimgurl;
        public String Headimgurl
        {
            get
            {
                return headimgurl;
            }
            set
            {
                headimgurl = value;
                RaisePropertyChanged("Headimgurl");
            }
        }

        private String unionid;
        public String Unionid
        {
            get
            {
                return unionid;
            }
            set
            {
                unionid = value;
                RaisePropertyChanged("Unionid");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WUser> list = new List<WUser>();
            WUser item = new WUser();
            item.OpenID = OpenID;
            item.UserName = UserName;
            item.Password = Password;
            item.Nickname = Nickname;
            item.Province = Province;
            item.City = City;
            item.Country = Country;
            item.Headimgurl = Headimgurl;
            item.Unionid = Unionid;
            if (SexsSelected == "男")
            {
                item.Sex = 0;
            }
            else if (SexsSelected == "女")
            {
                item.Sex = 1;
            }
            else
            {
                item.Sex = 2;
            }
            list.Add(item);
            HttpResponse response = WUser.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
